﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Ninject.Web;
using Ninject;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using Legazpi.Payroll.DataAccess.DataObjects;
using Newtonsoft.Json;
using Legazpi.Payroll.Web.Models;

namespace Legazpi.Payroll.Web.Webservices
{
    /// <summary>
    /// Summary description for PayrollWebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class PayrollWebService : WebServiceBase
    {
        [Inject]
        public IPayrollDateService _payrollDateService { get; set; }

        [Inject]
        public IDayTimeRecordService _dayTimeRecordService { get; set; }

        [WebMethod]
        public string AddPayrollDate(string sPayDate)
        {
            //string userName = UserHelper.ValidateAndGetUserName();
            DateTime payDate ;
            if (!DateTime.TryParse(sPayDate, out payDate))
                return JsonConvert.SerializeObject(new { Success = false, Message = "Invalid Date" });
            if (!_payrollDateService.IsValidPaydate(payDate))
                return JsonConvert.SerializeObject(new { Success = false, Message = "Paydate day must start with 1 or 16" });

            var payrollDate = new PayrollDate();

            payrollDate.Paydate = payDate;
            _payrollDateService.Add(payrollDate);
            var status = _payrollDateService.Save();
            string json = JsonConvert.SerializeObject(new { Success = status });
            return json;
        }

        [WebMethod]
        public string SaveEmployeeDTR(DayTimeRecordObj dtrItem)
        {            
            var dayTimeRecord = _dayTimeRecordService.GetById(dtrItem.DayTimeRecordId);            
            var timeRecords = dtrItem.TimeRecords;
            bool isAdded = false; ;
            if (dayTimeRecord != null)
            {

            }
            else 
            {
                isAdded = true;
                dayTimeRecord = new DayTimeRecord()
                {
                    EmployeeId = dtrItem.EmployeeId,
                    PayrollDateId = dtrItem.PayrollDateId,                    
                };            
            }
            
            dayTimeRecord.LogTimeRecords = JsonConvert.SerializeObject(timeRecords);

            if (isAdded)
                _dayTimeRecordService.Add(dayTimeRecord);
            else
                _dayTimeRecordService.Update(dayTimeRecord);
            
            var status = _dayTimeRecordService.Save();
            dtrItem.DayTimeRecordId = dayTimeRecord.Id;            
            string json = JsonConvert.SerializeObject(
                new
                {
                    Success = status,
                    IsAdded = isAdded,
                    DtrItem = dtrItem,                    
                    WorkHours = dayTimeRecord.RegularTimeHours + dayTimeRecord.LegalHolidayTimeHours + dayTimeRecord.SpecialHolidayTimeHours,
                    NightDiff = dayTimeRecord.TotalNightDifferential,
                    OverTime = dayTimeRecord.TotalOverTime
                });
            return json;

        }

    }
    #region Models
    public class DayTimeRecordObj
    {
        public List<TimeRecord> TimeRecords { get; set; }
        public int EmployeeId { get; set; }
        public int PayrollDateId { get; set; }
        public int DayTimeRecordId { get; set; }
    }
    #endregion
}
